package com.jader.activities;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jader.beans.Goods;
import com.jader.beans.Shopcart;
import com.jader.fragment.Seller_goods_fragment;
import com.jader.myviews.BabyPopWindow;
import com.jader.myviews.MyProgressDialog;
import com.jader.util.JaderApplication;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.zlp.jader.R;

public class Seller extends Activity implements OnClickListener {

	private TextView put_in, buy_now;
	private BabyPopWindow popWindow;
	private LinearLayout all_choice_layout;
	private int isClickBuy;
	private FragmentManager manager;
	private FragmentTransaction transaction;
	private Seller_goods_fragment fragment;
	private MenuItem menuItem;
	private Goods goods;
	private TextView enter, phone_to_sellerman;
	private String url = JaderApplication.getIP() + "/Jader/client";
	private MyProgressDialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_seller);
		initViews();

	}

	private void initViews() {
		put_in = (TextView) findViewById(R.id.put_in);
		buy_now = (TextView) findViewById(R.id.buy_now);
		enter = (TextView) findViewById(R.id.enter_into);
		all_choice_layout = (LinearLayout) findViewById(R.id.all_choice_layout);
		phone_to_sellerman = (TextView) findViewById(R.id.lianxi_lefu);
		popWindow = new BabyPopWindow(this);
		put_in.setOnClickListener(this);
		buy_now.setOnClickListener(this);
		goods = (Goods) getIntent().getSerializableExtra("goods");
		popWindow
				.setOnItemClickListener(new BabyPopWindow.OnItemClickListener() {

					@Override
					public void onClickOKPop() {
						setBackgroundBlack(all_choice_layout, 1);
						isClickBuy(isClickBuy);
					}
				});
		manager = getFragmentManager();
		fragment = new Seller_goods_fragment();
		transaction = manager.beginTransaction();
		transaction.replace(R.id.frame, fragment);
		transaction.commit();
		enter.setOnClickListener(fragment);
		phone_to_sellerman.setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.iv_back:
			finish();
			break;
		case R.id.put_in:
			this.isClickBuy = 1;
			setBackgroundBlack(all_choice_layout, 0);
			popWindow.showAsDropDown(arg0);
			break;
		case R.id.buy_now:
			this.isClickBuy = 2;
			setBackgroundBlack(all_choice_layout, 0);
			popWindow.showAsDropDown(arg0);
			break;
		case R.id.lianxi_lefu:
			Toast.makeText(this, "打电话", Toast.LENGTH_LONG).show();
			String number ="19756456489";
			Intent intent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:"+number));  
            startActivity(intent);  
			break;

		default:
			break;
		}
	}

	public void setBackgroundBlack(View view, int what) {
		switch (what) {
		case 0:
			view.setVisibility(View.VISIBLE);
			break;
		case 1:
			view.setVisibility(View.GONE);
			break;
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (!popWindow.isShowing()) {
			isClickBuy = 1;
		}
		return super.onTouchEvent(event);

	}

	public void setClickBuy(int isClickBuy) {
		this.isClickBuy = isClickBuy;
	}

	public void openEvalute(int goodsid) {
		Intent intent = new Intent(Seller.this, Evalute.class);
		intent.putExtra("goodsid", goodsid);
		startActivity(intent);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.store, menu);
		menuItem = menu.findItem(R.id.action_exits);
		menuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {

			@Override
			public boolean onMenuItemClick(MenuItem arg0) {
				Intent intent = new Intent(Intent.ACTION_MAIN);
				intent.addCategory(Intent.CATEGORY_HOME);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
				startActivity(intent);
				finish();
				return false;
			}
		});
		return true;
	}

	public BabyPopWindow getPopWindow() {
		return popWindow;
	}

	public void isClickBuy(int number) {
		switch (number) {
		case 1:
			if (JaderApplication.user == null) {
				JaderApplication.login(this);
			} else {
				RequestParams params = new RequestParams();
				params.addBodyParameter("type",
						JaderApplication.INSERT_GOODS_TOSHOPCAT + "");
				Shopcart shopcart = new Shopcart(0,
						JaderApplication.user.getUserid(), goods.getGoodsid(),
						popWindow.getNumber());
				params.addBodyParameter("insertshopcat",
						new Gson().toJson(shopcart));
				insertStopcat(url, params);
			}
			break;
		case 2:
			if (JaderApplication.user == null) {
				JaderApplication.login(this);
			} else {
				Intent intent = new Intent(Seller.this, BuyNow.class);
				intent.putExtra("number", popWindow.getNumber());
				intent.putExtra("goods", goods);
				intent.putExtra("userid", JaderApplication.user.getUserid());
				startActivity(intent);
			}
			break;
		default:
			break;
		}
	}

	private void insertStopcat(String url, RequestParams params) {
		dialog = JaderApplication.getMyDialog(this);
		dialog.show();
		JaderApplication.httpUtils.send(HttpMethod.POST, url, params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						JaderApplication.show
						(Seller.this, "访问网络失败");
						if (dialog.isShowing()) {
							dialog.dismiss();
						}
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						String result = arg0.result;
						if (!JaderApplication.isEmpty(result)) {
							JaderApplication.show(Seller.this, "添加成功");
						} else {
							JaderApplication.show(Seller.this, "操作失败，请重试！");
						}
						if (dialog.isShowing()) {
							dialog.dismiss();
						}
					}
				});
	}

}
